import { Component, OnInit, Input } from '@angular/core';
import {
    ScanningResultService,
    VulnerabilityItem,
    VulnerabilitySeverity
} from '../service/index';
import { ErrorHandler } from '../error-handler/index';
import { forkJoin } from "rxjs";

import { ChannelService } from "../channel/channel.service";
import { UserPermissionService } from "../service/permission.service";
import { USERSTATICPERMISSION } from "../service/permission-static";
@Component({
    selector: 'hbr-vulnerabilities-grid',
    templateUrl: './result-grid.component.html',
    styleUrls: ['./scanning.scss']
})
export class ResultGridComponent implements OnInit {
    scanningResults: VulnerabilityItem[] = [];
    dataCache: VulnerabilityItem[] = [];

    @Input() tagId: string;
    @Input() repositoryId: string;
    @Input() projectId: number;
    hasScanImagePermission: boolean;
    constructor(
        private scanningService: ScanningResultService,
        private channel: ChannelService,
        private userPermissionService: UserPermissionService,
        private errorHandler: ErrorHandler,
    ) { }

    ngOnInit(): void {
        this.loadResults(this.repositoryId, this.tagId);
        this.getScanPermissions(this.projectId);
    }

    loadResults(repositoryId: string, tagId: string): void {
        this.scanningService.getVulnerabilityScanningResults(repositoryId, tagId)
            .subscribe((results: VulnerabilityItem[]) => {
                this.dataCache = results;
                this.scanningResults = this.dataCache.filter((item: VulnerabilityItem) => item.id !== '');
            }, error => { this.errorHandler.error(error); });
    }

    // TODO: Should query from back-end service
    filterVulnerabilities(terms: string): void {
        if (terms.trim() === '') {
            this.scanningResults = this.dataCache.filter((item: VulnerabilityItem) => item.id !== '');
        } else {
            this.scanningResults = this.dataCache.filter((item: VulnerabilityItem) => this._regexpFilter(terms, item.package));
        }
    }

    refresh(): void {
        this.loadResults(this.repositoryId, this.tagId);
    }

    severityText(severity: VulnerabilitySeverity): string {
        switch (severity) {
            case VulnerabilitySeverity.HIGH:
                return 'VULNERABILITY.SEVERITY.HIGH';
            case VulnerabilitySeverity.MEDIUM:
                return 'VULNERABILITY.SEVERITY.MEDIUM';
            case VulnerabilitySeverity.LOW:
                return 'VULNERABILITY.SEVERITY.LOW';
            case VulnerabilitySeverity.NONE:
                return 'VULNERABILITY.SEVERITY.NEGLIGIBLE';
            case VulnerabilitySeverity.UNKNOWN:
                return 'VULNERABILITY.SEVERITY.UNKNOWN';
            default:
                return 'UNKNOWN';
        }
    }

    _regexpFilter(terms: string, testedValue: any): boolean {
        let reg = new RegExp('.*' + terms + '.*', 'i');
        return reg.test(testedValue);
    }

    scanNow(): void {
        this.channel.publishScanEvent(this.repositoryId + "/" + this.tagId);
    }
    getScanPermissions(projectId: number): void {

        const hasScanImagePermission = this.userPermissionService.getPermission(projectId,
            USERSTATICPERMISSION.REPOSITORY_TAG_SCAN_JOB.KEY, USERSTATICPERMISSION.REPOSITORY_TAG_SCAN_JOB.VALUE.CREATE);
        forkJoin(hasScanImagePermission).subscribe(permissions => {
            this.hasScanImagePermission = permissions[0] as boolean;
        }, error => {
            this.errorHandler.error(error);
        });
    }
}
